package mx.ipn.presentacion.facturacion;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.*;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterJob;
import java.awt.print.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.io.*;
public class View extends JFrame implements Printable, ActionListener{
	JButton imprime = new JButton("Imprime");
	//JButton guarda = new JButton("guarda");
	JTable tabla1 =  new JTable(4,2);
	TableColumn colum      = null;
	public String contenido="";
	//File fichero = new File("c:\\documents and settings\\uda\\mis documentos\\rep.rtf");
	    public View(String contenido)
	    {
	        try
	        {
	        	/*JFrame f = new JFrame();
	        	JPanel panel = new JPanel();
	        	panel.add(imprime);
	        	f.getContentPane().add(BorderLayout.SOUTH, panel);
	        	f.getContentPane().add(BorderLayout.CENTER, new View(contenido));
	        	f.setSize(580, 500);
	        	f.setVisible(true);*/
	            // Creación de la ventana de ejemplo
	            JFrame v = new JFrame("JTextPane");
	            JPanel contenedor = (JPanel) this.getContentPane();
	            JTextPane editor = new JTextPane();
	            JScrollPane scroll = new JScrollPane(editor);
	            //FlowLayout fl = new FlowLayout();
	            GridBagLayout gbl = new GridBagLayout();
	            GridBagConstraints gbc = new GridBagConstraints();
	            gbc.fill = GridBagConstraints.BOTH;
	            gbc.gridx = 0;
	            gbc.gridy = 1;
	            gbl.setConstraints( scroll,gbc );
	            gbc.fill = GridBagConstraints.NONE;
	            //gbc.gridwidth = GridBagConstraints.RELATIVE;
	            gbc.gridx = 0;
	            gbc.gridy = 2;
	            //gbc.gridy=GridBagConstraints.RELATIVE ;
	            gbl.setConstraints( imprime,gbc );
	    		contenedor.setLayout(gbl); 
	            this.setBounds(10, 10,800,800);
	            //scroll.setBounds(10, 10,400,400);
	            imprime.setSize(200,100);
	            this.getContentPane().add(scroll);
	            this.getContentPane().add(imprime);
	            //this.getContentPane().add(guarda);
	            v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	            /*tabla1.setBounds(10,110,400,50);
	    		tabla1.setRowHeight(25);
	    		tabla1.setValueAt("Conceptos", 0, 0);
	    		tabla1.setValueAt("Importes",0, 1);
	    		tabla1.setGridColor(new Color(0,0,1));
	    		tabla1.setBackground(new Color(0,0,1));
	    		colum = tabla1.getColumnModel().getColumn(0);
	    		colum.setPreferredWidth(200);*/ 
	            /*BufferedReader reader = new BufferedReader(new FileReader(fichero));*/
	            editor.setContentType("text/html");
	            editor.setText(contenido);
	            imprime.addActionListener(this);
	            /*SimpleAttributeSet attrs = new SimpleAttributeSet();
	            StringBuffer lineaTotal= new StringBuffer();
	            String linea = reader.readLine();
	            while (linea != null)
	            {
	               lineaTotal.append(linea);
	               lineaTotal.append(System.getProperty("line.separator")); 
	               linea = reader.readLine();
	            } 
	            editor.setText(lineaTotal.toString());
	            reader.close();*/

	            //Por supuesto, no se nos debe olvidar cerrar el fichero cuando terminemos

	            //reader.close();

	            // Atributos para cada uno de las frases.
	            /*SimpleAttributeSet attrs = new SimpleAttributeSet();

	            // En negrita
	            StyleConstants.setBold(attrs, true);
	            editor.getStyledDocument()
	                  .insertString(
	                editor.getStyledDocument().getLength(), "Negrita", attrs);
	            insertaNuevaLinea(editor);

	            // En cursiva
	            StyleConstants.setItalic(attrs, true);
	            StyleConstants.setBold(attrs, false);
	            editor.getStyledDocument()
	                  .insertString(
	                editor.getStyledDocument().getLength(), "cursiva", attrs);
	            insertaNuevaLinea(editor);

	            // Ponemos el cursor al final del texto
	            editor.setCaretPosition(editor.getStyledDocument().getLength());

	            // Un icono
	            ImageIcon icono = new ImageIcon("d:/viejo.gif");
	            editor.insertIcon(icono);
	            insertaNuevaLinea(editor);*/

	            // Ponemos el cursor al final del texto
	            editor.setCaretPosition(editor.getStyledDocument().getLength());

	            // insertamos un JButton
	            //JButton boton = new JButton("Pulsame");
	            //editor.insertComponent(tabla1);

	            // Visualizamos la ventana.
	            //this.pack();
	            /*this.addWindowListener(new WindowAdapter(){
	    			public void windowClosing(WindowEvent eventObject)
	    			{
	    				
	    			}
	    		});*/
	            this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	            this.setVisible(true);
	            //v.pack();
	            //v.setVisible(true);
	            //Process p = Runtime.getRuntime().exec("C:\\Archivos de programa\\Adobe\\Reader 8.0\\Reader\\AcroRd32.exe  C:\\Documents and Settings\\UDA\\Mis documentos\\desweb\\Bulma-2338.pdf");
	             
	            //Process p = Runtime.getRuntime().exec("C:\\Archivos de programa\\Adobe\\Reader 8.0\\Reader\\AcroRd32.exe");
	            //Process p = Runtime.getRuntime().exec("C:\\Documents and Settings\\UDA\\Mis documentos\\desweb\\Bulma-2338.pdf");
	            //p.waitFor();
	        }
	        catch (Exception e)
	        {
	            e.printStackTrace();
	        }
	    }
	    public void actionPerformed(ActionEvent e) {
	        if (e.getSource() instanceof JButton) {   
	        PrinterJob printJob = PrinterJob.getPrinterJob();
	        printJob.setPrintable(this);
	        if (printJob.printDialog()) {
	            try {
	                printJob.print();  
	            } catch (Exception ex) {
	                ex.printStackTrace();
	            }
	        }
		    }
	    }
	    /*public void paintComponent(Graphics g) {
	    	super.paintComponent(g);
	    }*/
	    public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
	        if (pi >= 1) {
	            return Printable.NO_SUCH_PAGE;
		}
		//drawShapes((Graphics2D) g);
	        return Printable.PAGE_EXISTS;
	    }
	    public static void main(String[] args)
	    {
	        new View("");
	    }
	} 
